from fastapi import APIRouter, Query, HTTPException
from orm.models import *
from pydantic import BaseModel, Field, field_validator
from tortoise.exceptions import DoesNotExist
from fastapi import status
from common.error_codes import ErrorCode

api_employee_query = APIRouter()


@api_employee_query.get('/query/employee_list', status_code=status.HTTP_200_OK)
async def get_employee_info(employee_name: str = Query(..., title="员工姓名")):
    if employee_name:
        #如果传了employee_name，就获取该员工信息
        try:
            user = await Employee.filter(name=employee_name).first()
            if not user:
                return {
                    'code':
                    ErrorCode.FailQueryEmployee,
                    "message":
                    ErrorCode.get_error_message(ErrorCode.FailQueryEmployee)
                }
            return {
                "code": status.HTTP_200_OK,
                "message": ErrorCode.get_error_message(ErrorCode.success),
                "data": user
            }
        except Exception as e:
            return {'code': 1001, "message": "QueryEmployeeError"}
    else:
        #如果不传employee_name，就获取所有员工信息
        employee_info = await Employee.all()
        return {
            'code': status.HTTP_200_OK,
            'message': 'success',
            'data': employee_info
        }
